package com.woyou.ui.component;

import java.io.File;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.citaq.ideliver.R;
import com.woyou.service.NetWorkCenter;
import com.woyou.utils.FileUtils;
import com.woyou.utils.Utils;

public class EditDialog implements OnClickListener {
	EditText et;
	RadioButton radiobtnOne, radiobtnTow; // 男、女单选按钮
	Button ok_button, cancle_button, take_photo; // 确定、取消
	TextView dialogTitle, phone, radiotextOne, radiotextTow, mintitle;
	RadioGroup radioGroup1;
	LinearLayout dialog_editLayout, nan, nv, edit_dialog;

	Window window;
	Dialog dialog;
	com.woyou.utils.Dialog showDialog = new com.woyou.utils.Dialog();
	AlertDialog dlg1 = null;

	StringBuilder sb = new StringBuilder();

	public void Dialog(final Context con, final TextView textArray,
			final int ID, String title, String state) {
		if (dialog != null && dialog.isShowing()) {
			return;
		}
		LayoutInflater factory = LayoutInflater.from(con);
		// 得到自定义对话框
		final View DialogView = factory.inflate(R.layout.pop_userinfodialog,
				null);
		dialog = new Dialog(con, R.style.FullHeightDialog);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.BOTTOM);

		// 姓名输入框
		dialog_editLayout = (LinearLayout) DialogView.findViewById(R.id.dialog_editLayout);
		dialogTitle = (TextView) DialogView.findViewById(R.id.string_textView);
		et = (EditText) DialogView.findViewById(R.id.dialog_editText);
		ok_button = (Button) DialogView.findViewById(R.id.ok_button);
		cancle_button = (Button) DialogView.findViewById(R.id.cancle_button);

		// 性别选择
		radioGroup1 = (RadioGroup) DialogView.findViewById(R.id.radioGroup1);
		nan = (LinearLayout) DialogView.findViewById(R.id.nan);
		nv = (LinearLayout) DialogView.findViewById(R.id.nv);
		radiobtnOne = (RadioButton) DialogView.findViewById(R.id.nan_radiobtn);
		radiobtnTow = (RadioButton) DialogView.findViewById(R.id.nv_radiobtn);
		radiotextOne = (TextView) DialogView.findViewById(R.id.radiotextOne);
		radiotextTow = (TextView) DialogView.findViewById(R.id.radiotextTow);
		radiobtnOne.setOnClickListener(this);
		nan.setOnClickListener(this);
		radiobtnTow.setOnClickListener(this);
		nv.setOnClickListener(this);

		// 提示框
		edit_dialog = (LinearLayout) DialogView.findViewById(R.id.edit_dialog);
		mintitle = (TextView) DialogView.findViewById(R.id.mintitle);
		phone = (TextView) DialogView.findViewById(R.id.phone);

		// 判断进入的为列表选择还是键盘输入true：键盘输入 false：选项选择
		if (state.equals("isEdit")) {
			// 初始化输入框
			radioGroup1.setVisibility(View.GONE);
			edit_dialog.setVisibility(View.GONE);
			et.setText(textArray.getText().toString());
			dialogTitle.setText(title);
			et.setSelection(textArray.getText().toString().length());
			et.addTextChangedListener(mTextWatcher);
			sb = sb.delete(0, sb.length());
		} else if (state.equals("isRadio")) {
			// 初始化性别单选项
			dialog_editLayout.setVisibility(View.GONE);
			et.setVisibility(View.GONE);
			edit_dialog.setVisibility(View.GONE);
			dialogTitle.setText(title);
			switch (ID) {
			case R.id.fm_myinfo_sextext:
				radiotextOne.setText("男");
				radiotextTow.setText("女");
				if (textArray.getText().toString().equals("男")) {
					radiobtnOne.setChecked(true);
					radiobtnOne.setBackgroundResource(R.raw.rediobox1);
				} else if (textArray.getText().toString().equals("女")) {
					radiobtnTow.setChecked(true);
					radiobtnTow.setBackgroundResource(R.raw.rediobox1);
				}
				break;
			case R.id.fm_set_picqualitytext:
				radiotextOne.setText("普通");
				radiotextTow.setText("清晰");
				if (textArray.getText().toString().equals("普通")) {
					radiobtnOne.setChecked(true);
					radiobtnOne.setBackgroundResource(R.raw.rediobox1);
				} else if (textArray.getText().toString().equals("清晰")) {
					radiobtnTow.setChecked(true);
					radiobtnTow.setBackgroundResource(R.raw.rediobox1);
				}
				break;
			default:
				break;
			}
		} else if (state.equals("isDialog")) {
			// 初始化提示框
			dialogTitle.setVisibility(View.GONE);
			radioGroup1.setVisibility(View.GONE);
			dialog_editLayout.setVisibility(View.GONE);
			et.setVisibility(View.GONE);
			switch (ID) {
			case R.id.fm_myinfo_phonetext:
				phone.setText(textArray.getText().toString());
				break;
			case R.id.fm_set_removepictext:
				mintitle.setText(title);
				break;
			case R.id.deletetext:
				mintitle.setText(title);
				break;
			case R.id.head_title:
				mintitle.setText(title);
				break;
			case R.id.problemcontext:
				mintitle.setText(title);
				break;
			case R.id.serviceproblemcontext:
				mintitle.setText(title);
				break;
			default:
				break;
			}
		}

		ok_button.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				hideSoftInput(con, et);
				switch (ID) {
				// 用户信息更新界面 姓名输入
				case R.id.fm_myinfo_nametext:
					if (et.getText().toString().length() != 0) {
						if (et.getText().toString().length() > 10) {
							Toast.makeText(con, "输入的字符过长", 2).show();
						} else {
							if (!NetWorkCenter.isNetworkConnected(con)) {
								Toast.makeText(con, "请检查你的网络!", 2).show();
								return;
							}
							textArray.setText(et.getText().toString());
							if (listener != null) {
								listener.pressBtn(R.id.ok_button,
										"fm_myinfo_nametext");
							}
							dialog.dismiss();
						}
					} else {
						Toast.makeText(con, "姓名不能为空", 1).show();
					}
					break;
				// 用户信息更新界面 性别选择
				case R.id.fm_myinfo_sextext:
					if (radiobtnOne.isChecked()) {
						if (!NetWorkCenter.isNetworkConnected(con)) {
							Toast.makeText(con, "请检查你的网络!", 2).show();
							;
							return;
						}
						textArray.setText("男");
						dialog.dismiss();
					} else {
						if (!NetWorkCenter.isNetworkConnected(con)) {
							Toast.makeText(con, "请检查你的网络!", 2).show();
							;
							return;
						}
						textArray.setText("女");
						dialog.dismiss();
					}
					if (listener != null) {
						listener.pressBtn(R.id.ok_button, "fm_myinfo_sextext");
					}
					break;
				// 退出当前账号
				case R.id.fm_myinfo_phonetext:
					if (listener != null) {
						listener.pressBtn(R.id.ok_button,
								"fm_myinfo_backaccountbtn");
					}
					dialog.dismiss();
					break;
				// 设置界面清空图片缓存
				case R.id.fm_set_removepictext:
					// 清除缓存图片代码 TODO
					File file = Utils.createDefaultCacheDir(con);
					delAllFile(file.getPath());
					textArray.setText(FileUtils.getDiskCacheSize(con));
					dialog.dismiss();
					break;
				// 设置界面更改图片清晰度
				case R.id.fm_set_picqualitytext:
					if (radiobtnOne.isChecked()) {
						textArray.setText("普通");
						dialog.dismiss();
					} else {
						textArray.setText("清晰");
						dialog.dismiss();
					}
					Toast.makeText(con,
							"已将非wifi下图片质量设为：" + textArray.getText().toString(),
							1).show();
					break;
				case R.id.deletetext:
					// 添加地址界面删除地址
					if (listener != null) {
						listener.pressBtn(R.id.ok_button, "putaddr_delete");
					}
					dialog.dismiss();
					break;
				// 退出管理配送地址界面
				case R.id.head_title:
					if (listener != null) {
						listener.pressBtn(R.id.ok_button, "putaddr_back");
					}
					dialog.dismiss();
					break;
				// 退出APP意见反馈界面
				case R.id.problemcontext:
					if (listener != null) {
						listener.pressBtn(R.id.ok_button, "appproblem_back");
					}
					dialog.dismiss();
					break;
					// 退出APP意见反馈界面
				case R.id.serviceproblemcontext:
					if (listener != null) {
						listener.pressBtn(R.id.ok_button, "serviceproblem_back");
					}
					dialog.dismiss();
					break;
				default:
					break;

				}
			}
		});
		cancle_button.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				hideSoftInput(con, et);
				dialog.dismiss();
			}
		});
		dialog.setContentView(DialogView);
		dialog.setCancelable(true);
		dialog.show();

		Window win = dialog.getWindow();
		win.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		win.setAttributes(lp);
	}
	
	public void dismiss(){
		if ( dialog!=null && dialog.isShowing() ) {
			dialog.dismiss();
		}
	}
	
	private BtnListener listener;

	public interface BtnListener {
		void pressBtn(int id, String str);
	}

	public void setBtnListener(BtnListener l) {
		this.listener = l;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.nan:
			setRbCheck(radiobtnOne, radiobtnTow);
			break;
		case R.id.nv:
			setRbCheck(radiobtnTow, radiobtnOne);
			break;
		case R.id.nan_radiobtn:
			setRbCheck(radiobtnOne, radiobtnTow);
			break;
		case R.id.nv_radiobtn:
			setRbCheck(radiobtnTow, radiobtnOne);
			break;
		default:
			break;
		}
	}

	// 单选按钮切换
	public void setRbCheck(final RadioButton rbTrue, final RadioButton rbFlase) {
		rbTrue.setChecked(true);
		rbTrue.setBackgroundResource(R.raw.rediobox1);
		rbFlase.setChecked(false);
		rbFlase.setBackgroundResource(R.raw.rediobox2);
	}

	/**
	 * @方法 TextWatcher 监听EditText输入加载
	 * @说明 获取Edittext中字符长度显示
	 */
	TextWatcher mTextWatcher = new TextWatcher() {
		private boolean flag = false;

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (flag) {
				return;
			}
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			if (flag) {
				return;
			}
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (flag) {
				flag = false;
				return;
			}
			if (et.getText().toString().length() > 8) {
				flag = true;
				et.setText(et.getText().toString().substring(0, 8));
				et.setSelection(8);
			}
		}
	};
	
	/**
	 * 隐藏输入法
	 * 
	 * @param context
	 * @param achor
	 */
	public static void hideSoftInput(Context context, View achor) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(achor.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}
	

	// 删除指定文件夹下所有文件
	// param path 文件夹完整绝对路径
	public static boolean delAllFile(String path) {
		boolean flag = false;
		File file = new File(path);
		if (!file.exists()) {
			return flag;
		}
		if (!file.isDirectory()) {
			return flag;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
				flag = true;
			}
		}
		return flag;
	}

	// 删除文件夹
	// param folderPath 文件夹完整绝对路径

	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			java.io.File myFilePath = new java.io.File(filePath);
			myFilePath.delete(); // 删除空文件夹
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
